#coding:utf-8
from . import main 
from flask import render_template,session,request,redirect,url_for
from .. import model ,db
import datetime,random

@main.route('/postr')
def postr():
   
    users=db.session.query(model.Kinds).first()
    # id = users.id
    # cla=db.session.query(model.Kinds).filter(model.Kinds.id==kna).first()
    # couses=cla.couses
    # sada=session.get('sada')
    # name=session.get('name')
    # print  sada,name
    return  render_template('host.html',sada=session.get('sada'),name=session.get('name'),users=users,id = users.id)

# 点击课程看视频
@main.route('/course/<kna>')
def course(kna):
    users=db.session.query(model.Kinds).all()
    cla=db.session.query(model.Kinds).filter(model.Kinds.id==kna).first()
    couses=cla.couses
    # sada=session.get('sada')
    # name=session.get('name')
    # print  sada,name
    return render_template('djymbh.html',couses=couses,sada=session.get('sada'),name=session.get('name'),users=users,cla=cla,id=cla.id)



# 课程

@main.route('/video/<id>')
def video(id):
    user=db.session.query(model.Courses).filter(model.Courses.id==id).first()
    videos=user.videos
    users=db.session.query(model.Kinds).first()

    # sada=session.get('sada')
    # name=session.get('name')
    # print  sada,name
    return render_template('video.html',sada=session.get('sada'),name=session.get('name'),videos=videos,user=user,id=users.id)



# # 删除 
@main.route('/delete',methods=['POST'])
def delete():
    id=request.form['id']
    cla=db.session.query(model.Courses).filter(model.Courses.id==id).first()
    db.session.delete(cla)
    db.session.commit()
    # cla=db.session.query(model)

    return ' sussion'


#编辑
@main.route('/revise/<id>')
def revise(id):
    user=db.session.query(model.Courses).filter(model.Courses.id==id).first()
    # sada=session.get('sada')
    # name=session.get('name')
    # print  sada,name
    users=db.session.query(model.Kinds).first()
    return render_template('revise.html',sada=session.get('sada'),name=session.get('name'),user=user,id=users.id)



# 课程修改  l
@main.route('/amend/<id>',methods=['POST'])
def amend(id):
    # id=request.form['id']
    title=request.form['name']
    abstract=request.form['intro']
    user=db.session.query(model.Courses).filter(model.Courses.id==id).first()
    user.title=title
    user.abstract=abstract
    cla=model.Courses(title=user.title,abstract=user.abstract)
    db.session.add(cla)
    db.session.commit()
    # print '/////'
    # print id,user.title,user.abstract
    sada=session.get('sada')
    name=session.get('name')
    return  redirect(url_for('main.postr'))

@main.route('/teachers')
def teachers():
    cla=db.session.query(model.Kinds).first()
    user=db.session.query(model.Teacher).all()
    # sada=session.get('sada')
    # name=session.get('name')
    # print  sada,name
    return render_template('teacher_table.html',user=user,id=cla.id,sada=session.get('sada'),name=session.get('name'))

# 点击进入课程添加页面
@main.route('/accretion')
def accretion():
    # sada=session.get('sada')
    # name=session.get('name')
    # print  sada,name
    users=db.session.query(model.Kinds).first()
    return render_template('accretion.html',sada=session.get('sada'),name=session.get('name'),id=users.id)


# 课程添加
@main.route('/courseadd',methods=['POST'])
def courseadd():
    title=request.form['name']
    abstract=request.form['intro']
    addtime=datetime.datetime.now()
    k_id=random.randint(1,8)
    user=model.Courses(title=title,abstract=abstract,addtime=addtime,k_id=k_id)
    db.session.add(user)
    db.session.commit()
    return  redirect(url_for('main.postr'))


# 视频添加页面
@main.route('/addtv/<var>')
def addtv(var):
    tname=db.session.query(model.Teacher).all()
    users=db.session.query(model.Kinds).first()
    num=db.session.query(model.Teacher).filter(model.Teacher.id==var).first()
    return render_template('addtv.html',sada=session.get('sada'),name=session.get('name'),tname=tname,cla=num,var=var,id=users.id)
# # 视频添加
@main.route('/addtvs/<var>',methods=['POST'])
def addtvs(var):
    title=request.form['name']
    abstract=request.form['intro']
    t_id=random.randint(1,8)
    time=request.form['time']
    addtime=datetime.datetime.now()
    hist=random.randint(80,100)
    c_id=var
    user=model.Videos(title=title,abstract=abstract,t_id=t_id,time=time,addtime=addtime,c_id=c_id,hist=hist)
    db.session.add(user)
    db.session.commit()
    return  redirect(url_for('main.video',id=var))


#编辑视频
@main.route('/tvrevise/<id>')
def tvrevise(id):
    tname=db.session.query(model.Teacher).all()
    users=db.session.query(model.Kinds).first()
    user=db.session.query(model.Videos).filter(model.Videos.id==id).first()
    return render_template('tvrevised.html',user=user,sada=session.get('sada'),name=session.get('name'),id=users.id,tname=tname)

# 确认修改
@main.route('/affirm/<id>',methods=['POST'])
def affirm(id):
    title=request.form['name']
    abstract=request.form['intro'] 
    time=request.form['time'] 
    addtime=datetime.datetime.now()
    cla=db.session.query(model.Videos).filter(model.Videos.id==id).first()
    cla.title=title
    cla.abstract=abstract
    cla.time=time
    user=model.Videos(title=cla.title,abstract=cla.abstract,time=cla.time,addtime=addtime)
    db.session.add(user)
    db.session.commit()
    return redirect(url_for('main.course',kna=1))

@main.route('/tvdelete',methods=['POST'])
def tvdelete():
     id=request.form['id']
     deltv=db.session.query(model.Videos).filter(model.Videos.id==id).first()
     db.session.delete(deltv)
     db.session.commit()
     return ' sussion'



# 教师修改
@main.route('/teacadit/<id>')
def teacadit(id):
    users=db.session.query(model.Kinds).first()
    user=db.session.query(model.Teacher).filter(model.Teacher.id==id).first()
    return render_template('teacadit.html',user=user,sada=session.get('sada'),name=session.get('name'),id=users.id)
# 确认老师修改
@main.route('/tracrevi/<id>',methods=['POST'])
def teacrevi(id):
    name=request.form['name']
    abstract=request.form['intro'] 
    level=request.form['prof']
    user=db.session.query(model.Teacher).filter(model.Teacher.id==id).first()
    user.name=name
    user.abstract=abstract
    user.level=level
    add=model.Teacher(name=user.name,abstract=user.abstract,level=user.level)
    db.session.add(add)
    db.session.commit()
    return redirect(url_for('main.teachers'))

# 添加教师信息
@main.route('/addteacher')
def addteacher():
    users=db.session.query(model.Kinds).first()
    return render_template('addteacher.html',sada=session.get('sada'),name=session.get('name'),id=users.id)
# 确认添加老师信息
@main.route('/addsucc',methods=['POST'])
def addsucc():
    name=request.form['name']
    abstract=request.form['intro']
    level=request.form['prof']
    teach=model.Teacher(name=name,abstract=abstract,level=level)
    db.session.add(teach)
    db.session.commit()
    return redirect(url_for('main.teachers'))

# 老师删除
@main.route('/laosdele',methods=['POST'])
def laosdele():
    id=request.form['id']
    teach=db.session.query(model.Teacher).filter(model.Teacher.id==id).first()
    db.session.delete(teach)
    db.session.commit()
    # return redirect(url_for('main.teachers'))
    return ' sussion'


   
    
   













